<!--
  * 联网设备NB类型表
  *
  * @Author:    cfsjiazy
  * @Date:      2025-09-15 09:35:31
  * @Copyright   
-->
<template>
  <a-modal
      :title="form.id ? '编辑' : '添加'"
      :width="800"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >
        <a-form-item label="所属单位"  name="companyCode">
          <a-input-number style="width: 100%" v-model:value="form.companyCode" placeholder="所属单位" />
        </a-form-item>
        <a-form-item label="设备名称"  name="monitorName">
          <a-input style="width: 100%" v-model:value="form.monitorName" placeholder="设备名称" />
        </a-form-item>
        <a-form-item label="设备编码"  name="monitorCode">
          <a-input style="width: 100%" v-model:value="form.monitorCode" placeholder="设备编码" />
        </a-form-item>
        <a-form-item label="所属系统(字典fire_system_type)"  name="system">
          <a-input style="width: 100%" v-model:value="form.system" placeholder="所属系统(字典fire_system_type)" />
        </a-form-item>
        <a-form-item label="设备厂商（字典cfs_data_equipment_manufacturer_type）"  name="dockCode">
          <a-input style="width: 100%" v-model:value="form.dockCode" placeholder="设备厂商（字典cfs_data_equipment_manufacturer_type）" />
        </a-form-item>
        <a-form-item label="设备类型（关联cfs_data_manufactor_device表device_code字段）"  name="deviceCode">
          <a-input style="width: 100%" v-model:value="form.deviceCode" placeholder="设备类型（关联cfs_data_manufactor_device表device_code字段）" />
        </a-form-item>
        <a-form-item label="设备型号（关联cfs_data_manufactor_device表id字段）"  name="monitorType">
          <a-input style="width: 100%" v-model:value="form.monitorType" placeholder="设备型号（关联cfs_data_manufactor_device表id字段）" />
        </a-form-item>
        <a-form-item label="运营商（1中国电信 2中国移动 3中国联通）"  name="commOperator">
          <a-input style="width: 100%" v-model:value="form.commOperator" placeholder="运营商（1中国电信 2中国移动 3中国联通）" />
        </a-form-item>
        <a-form-item label="通讯协议（TCP、UDP、HTTP）"  name="commProtocol">
          <a-input style="width: 100%" v-model:value="form.commProtocol" placeholder="通讯协议（TCP、UDP、HTTP）" />
        </a-form-item>
        <a-form-item label="IMEI"  name="imei">
          <a-input style="width: 100%" v-model:value="form.imei" placeholder="IMEI" />
        </a-form-item>
        <a-form-item label="iccid"  name="iccid">
          <a-input style="width: 100%" v-model:value="form.iccid" placeholder="iccid" />
        </a-form-item>
        <a-form-item label="装机日期"  name="installDate">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.installDate" style="width: 100%" placeholder="装机日期" />
        </a-form-item>
        <a-form-item label="安装位置"  name="installAddress">
          <a-input style="width: 100%" v-model:value="form.installAddress" placeholder="安装位置" />
        </a-form-item>
        <a-form-item label="入网时间"  name="networkDate">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.networkDate" style="width: 100%" placeholder="入网时间" />
        </a-form-item>
        <a-form-item label="服务到期时间"  name="servicedDate">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.servicedDate" style="width: 100%" placeholder="服务到期时间" />
        </a-form-item>
        <a-form-item label="运行状态（0正常 1故障 2报警）"  name="runState">
          <a-input style="width: 100%" v-model:value="form.runState" placeholder="运行状态（0正常 1故障 2报警）" />
        </a-form-item>
        <a-form-item label="经度"  name="lng">
          <a-input-number style="width: 100%" v-model:value="form.lng" placeholder="经度" />
        </a-form-item>
        <a-form-item label="纬度"  name="lat">
          <a-input-number style="width: 100%" v-model:value="form.lat" placeholder="纬度" />
        </a-form-item>
        <a-form-item label="所属消防网格"  name="fireGridId">
          <a-input-number style="width: 100%" v-model:value="form.fireGridId" placeholder="所属消防网格" />
        </a-form-item>
        <a-form-item label="监测的设备类型（字典cfs_data_monitor_type）"  name="monitoringType">
          <a-input style="width: 100%" v-model:value="form.monitoringType" placeholder="监测的设备类型（字典cfs_data_monitor_type）" />
        </a-form-item>
        <a-form-item label="对接app_id"  name="appId">
          <a-input style="width: 100%" v-model:value="form.appId" placeholder="对接app_id" />
        </a-form-item>
        <a-form-item label="第三方数据id"  name="dataId">
          <a-input style="width: 100%" v-model:value="form.dataId" placeholder="第三方数据id" />
        </a-form-item>
        <a-form-item label="状态"  name="status">
          <a-input-number style="width: 100%" v-model:value="form.status" placeholder="状态" />
        </a-form-item>
        <a-form-item label="创建时间"  name="createTime">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.createTime" style="width: 100%" placeholder="创建时间" />
        </a-form-item>
        <a-form-item label="创建人"  name="createUser">
          <a-input-number style="width: 100%" v-model:value="form.createUser" placeholder="创建人" />
        </a-form-item>
        <a-form-item label="修改时间"  name="updateTime">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.updateTime" style="width: 100%" placeholder="修改时间" />
        </a-form-item>
        <a-form-item label="修改人"  name="updateUser">
          <a-input-number style="width: 100%" v-model:value="form.updateUser" placeholder="修改人" />
        </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { cfsDataNbUsermonitorinfoApi } from '/@/api/cfs/cfs-data-nb-usermonitorinfo/cfs-data-nb-usermonitorinfo-api';
  import { smartSentry } from '/@/lib/smart-sentry';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      companyCode: undefined, //所属单位
      monitorName: undefined, //设备名称
      monitorCode: undefined, //设备编码
      system: undefined, //所属系统(字典fire_system_type)
      dockCode: undefined, //设备厂商（字典cfs_data_equipment_manufacturer_type）
      deviceCode: undefined, //设备类型（关联cfs_data_manufactor_device表device_code字段）
      monitorType: undefined, //设备型号（关联cfs_data_manufactor_device表id字段）
      commOperator: undefined, //运营商（1中国电信 2中国移动 3中国联通）
      commProtocol: undefined, //通讯协议（TCP、UDP、HTTP）
      imei: undefined, //IMEI
      iccid: undefined, //iccid
      installDate: undefined, //装机日期
      installAddress: undefined, //安装位置
      networkDate: undefined, //入网时间
      servicedDate: undefined, //服务到期时间
      runState: undefined, //运行状态（0正常 1故障 2报警）
      lng: undefined, //经度
      lat: undefined, //纬度
      fireGridId: undefined, //所属消防网格
      monitoringType: undefined, //监测的设备类型（字典cfs_data_monitor_type）
      appId: undefined, //对接app_id
      dataId: undefined, //第三方数据id
      status: undefined, //状态
      createTime: undefined, //创建时间
      createUser: undefined, //创建人
      updateTime: undefined, //修改时间
      updateUser: undefined, //修改人
  };

  let form = reactive({ ...formDefault });

  const rules = {
      companyCode: [{ required: true, message: '所属单位 必填' }],
      monitorName: [{ required: true, message: '设备名称 必填' }],
      monitorCode: [{ required: true, message: '设备编码 必填' }],
      system: [{ required: true, message: '所属系统(字典fire_system_type) 必填' }],
      dockCode: [{ required: true, message: '设备厂商（字典cfs_data_equipment_manufacturer_type） 必填' }],
      deviceCode: [{ required: true, message: '设备类型（关联cfs_data_manufactor_device表device_code字段） 必填' }],
      monitorType: [{ required: true, message: '设备型号（关联cfs_data_manufactor_device表id字段） 必填' }],
      commOperator: [{ required: true, message: '运营商（1中国电信 2中国移动 3中国联通） 必填' }],
      commProtocol: [{ required: true, message: '通讯协议（TCP、UDP、HTTP） 必填' }],
      imei: [{ required: true, message: 'IMEI 必填' }],
      iccid: [{ required: true, message: 'iccid 必填' }],
      installDate: [{ required: true, message: '装机日期 必填' }],
      installAddress: [{ required: true, message: '安装位置 必填' }],
      networkDate: [{ required: true, message: '入网时间 必填' }],
      servicedDate: [{ required: true, message: '服务到期时间 必填' }],
      runState: [{ required: true, message: '运行状态（0正常 1故障 2报警） 必填' }],
      lng: [{ required: true, message: '经度 必填' }],
      lat: [{ required: true, message: '纬度 必填' }],
      fireGridId: [{ required: true, message: '所属消防网格 必填' }],
      monitoringType: [{ required: true, message: '监测的设备类型（字典cfs_data_monitor_type） 必填' }],
      appId: [{ required: true, message: '对接app_id 必填' }],
      dataId: [{ required: true, message: '第三方数据id 必填' }],
      status: [{ required: true, message: '状态 必填' }],
      createTime: [{ required: true, message: '创建时间 必填' }],
      createUser: [{ required: true, message: '创建人 必填' }],
      updateTime: [{ required: true, message: '修改时间 必填' }],
      updateUser: [{ required: true, message: '修改人 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.id) {
        await cfsDataNbUsermonitorinfoApi.update(form);
      } else {
        await cfsDataNbUsermonitorinfoApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
